<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/calendar.js"></script>
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript"><!--

	function checkForm(){
		var grossBackCostIds = document.getElementsByName("grossBackCostId");
		var costs = document.getElementsByName("cost");
		var backCostTypeIds = document.getElementsByName("backCostTypeId");
		var backCostWayIds = document.getElementsByName("backCostWayId");
		var backDates = document.getElementsByName("backDate");
		var backCostDescs = document.getElementsByName("backCostDesc");
		var salePersons = document.getElementsByName("salePerson");
		
		var sign = 0;
		for(var i = 0; i < costs.length; i++){
			if(grossBackCostIds[i].value != 0 
				|| costs[i].value != "" 
				|| backCostTypeIds[i].value != 0 
				|| backCostWayIds[i].value != 0 
				//|| backDates[i].value != "" 
				|| backCostDescs[i].value != ""){ 
				//|| (salePersons[i] != undefined && salePersons[i].value != "")){
				if(grossBackCostIds[i].value == 0){
					alert("请选择回款类型！");
					return false;
				}
				if(costs[i].value == ""){
					alert("金额<fmt:message key='common.message.empty'/>");
					return false;
				}
				if(backCostTypeIds[i].value == 0){
					alert("请选择付款类型！");
					return false;
				}
				if(backCostWayIds[i].value == 0){
					alert("请选择付款方式！");
					return false;
				}
				/**if(backDates[i].value == ""){
					alert("回款日期<fmt:message key='common.message.empty'/>");
					return false;
				}
				if(salePersons[i] != undefined && salePersons[i].value == ""){
					alert("销售代表<fmt:message key='common.message.empty'/>");
					return false;
				}*/
			}
			
			if(grossBackCostIds[i].value != 0 
				&& costs[i].value != "" 
				&& backCostTypeIds[i].value != 0 
				&& backCostWayIds[i].value != 0){
				
				if(backDates[i].value == ""){
					alert("回款日期<fmt:message key='common.message.empty'/>");
					return false;
				}
				
				if(salePersons[i] != undefined && salePersons[i].value == ""){
					alert("销售代表<fmt:message key='common.message.empty'/>");
					return false;
				}
			}
			if(salePersons[i] == undefined){
				if(grossBackCostIds[i].value != 0 
					&& costs[i].value != "" 
					&& backCostTypeIds[i].value != 0 
					&& backCostWayIds[i].value != 0 
					&& backDates[i].value != ""){
						sign = sign + 1;
				}
			}else{
				if(grossBackCostIds[i].value != 0 
					&& costs[i].value != "" 
					&& backCostTypeIds[i].value != 0 
					&& backCostWayIds[i].value != 0 
					&& backDates[i].value != "" 
					&& salePersons[i].value != ""){
						sign = sign + 1;
				}
			}
		}
		if(sign <= 0){
			alert("回款记录不能为空，请追加回款记录！");
			return false;
		}
		return true;
	}
	
	function changeCheckbox(sindex,index){
		var selectValue = document.getElementsByName("grossBackCostId")[sindex].value;
		if(selectValue == '2'){
			document.getElementsByName("businessCheckbox")[index].disabled = false;
			document.getElementsByName("businessCheckbox")[index].checked = true;
			document.getElementsByName("businessStatus")[index].value = "1";
		}else{
			document.getElementsByName("businessCheckbox")[index].disabled = true;
			document.getElementsByName("businessCheckbox")[index].checked = false;
			document.getElementsByName("businessStatus")[index].value = "0";
		}
	}

	function changeValue(index){
		var businessCheckbox = document.getElementsByName("businessCheckbox")[index];
		if(businessCheckbox.checked){
			document.getElementsByName("businessStatus")[index].value = "1";
		}else{
			document.getElementsByName("businessStatus")[index].value = "0";
		}
	}
	
	function back(){
		var uri = "./contractBackCost.do";
		var sign = "<c:out value='${sign}'/>";
		if(sign == "1"){//返回到合同应收帐管理页面
			uri = "./contractBackCost.do";
		}else if(sign == "2"){//返回到部门合同应收帐管理页面
			uri = "./contractBackCost.do?action=deptBackCostManage";
		}else if(sign == "3"){//返回到部门所有合同页面
			uri = "./contract.do?action=preDeptContract";
		}else if(sign == "4"){//返回到我的历史合同页面
			uri = "./contract.do?action=preClosedContract";
		}
		window.location.href = uri;
		window.location.target = "main";
	}

	function personDialog(rowNum){
		if(confirm("请注意，修改销售代表将会导致业绩考核发生变动！")){
			var url = "./contract.do?action=selectSalePerson";
			var dlgParam = "dialogHeight:350px;dialogWidth:400px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
			var rt = window.showModalDialog(url,"",dlgParam);
			if(rt != undefined){
				//根据从子窗口得到的值，从中提取销售代表id和销售代表名称
				var salePersonId = "";
				var salePerson = "";
				var tStr = rt.split(",");
				if(tStr[0] != 0 && tStr[1] != 0){
					salePersonId = tStr[0];
					salePerson = tStr[1];
				}
				document.getElementsByName("salePersonId")[Number(rowNum) ].value = salePersonId;
				document.getElementsByName("salePerson")[Number(rowNum) ].value = salePerson;
				/**document.getElementById("salePersonId").value = salePersonId;										
				document.getElementById("salePerson").value = salePerson;
				*/
			}
		}
	}
	
	function save(){
		if(checkForm()){
			document.updateBackCostForm.action = "./contractBackCost.do?action=updateBackCost";
			document.updateBackCostForm.target = "main";
			document.updateBackCostForm.submit();
		}
	}

	
	function initPage(){
		/**if("<c:out value='${result}'/>" == 1){
			alert("<fmt:message key='common.message.addSuccess'/>");
		}*/
	}

--></script>
</head>
<body onload="initPage()">
<form name="updateBackCostForm" action="" method="post">
	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title1">
					<tr>
						<td class="title_lab">修改回款</td>
					</tr>
					<tr>
						<td align="center" valign="bottom">
							<button class="btn_submit" onclick="save()" hidefocus="true">提交</button>&nbsp;&nbsp;
							<button class="btn_back" onclick="back()" hidefocus="true">取消并返回</button>
						</td>
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<table class="table_add">
								<tr>
									<td class="title_top" width="150">已回款：${contract.backCost}元</td>
									
									<!--<td class="title_top"><button class="btn_append" onclick="addRow('table1')" hidefocus="true">追加行</button></td>
									 
								--></tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<table id="table1" class="table_style">
								<tr>
									<c:if test="${(isBusinessDeptBackCost == 1) || (isBusinessBackCost == 1)}">
									<td class="title" width="30">业绩</td>
									</c:if>
									<td class="title" width="10%">回款类型</td>
									<td class="title" width="12%">金额(元)</td>
									<td class="title" width="12%">收入类型</td>
									<td class="title" width="12%">付款类型</td>
									<td class="title" width="12%">付款方式</td>
									<td class="title" width="10%">回款日期</td>
									<td class="title" width="15%">销售代表</td>
									<td class="title" width="">备注</td>
								</tr>
								<c:forEach var="backCost" items="${backCosts}" varStatus="bc">
								<tr>
									<c:if test="${(isBusinessDeptBackCost == 1) || (isBusinessBackCost == 1)}">
									<td>
										<input id="businessCheckbox" name="businessCheckbox" type="checkbox" onclick="changeValue(${bc.index},${bc.index})" value="1" 
											<c:if test='${backCost.businessStatus == 1 && backCost.grossBackCostId == 2}'>checked</c:if> <c:if test='${backCost.grossBackCostId != 2}'>disabled</c:if> />
										<input id="businessStatus" name="businessStatus" type="hidden" value="${backCost.businessStatus}"/>
									
										<!--<input id="businessCheckbox" name="businessCheckbox" type="checkbox" value="1" disabled="disabled"/>
										<input id="businessStatus" name="businessStatus" type="hidden" value="0"/>-->
									</td>
									</c:if>
									<td>
										<select class="input_percent" id="grossBackCostId" name="grossBackCostId" onchange="changeCheckbox(${bc.index},${bc.index})">
											<option value="0" <c:if test='${backCost.grossBackCostId == 0}'>selected</c:if> >----请选择----</option>
											<option value="1" <c:if test='${backCost.grossBackCostId == 1}'>selected</c:if> >成本</option>
											<option value="2" <c:if test='${backCost.grossBackCostId == 2}'>selected</c:if> >毛利</option>
										</select>
										<input id="id" name="id" type="hidden" value="${backCost.id}"/>
									</td>
									<td><input id="cost" name="cost" class="input_percent" type="text" value="${backCost.cost}" onkeyup="onlyNumFloat(this)"/></td>
									<td>
										<select id="incomeTypeId" name="incomeTypeId" class="input_percent">
											<option value="0" <c:if test='${backCost.incomeTypeId == 0}'>selected</c:if> >-----请选择-----</option>
											<c:forEach var="range" items="${ranges['35']}">
											<option value="${range.id}" <c:if test='${backCost.incomeTypeId == range.id}'>selected</c:if> >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<select id="backCostTypeId" name="backCostTypeId" class="input_percent">
											<option value="0" <c:if test='${backCost.backCostTypeId == 0}'>selected</c:if> >-----请选择-----</option>
											<c:forEach var="range" items="${ranges['32']}">
											<option value="${range.id}" <c:if test='${backCost.backCostTypeId == range.id}'>selected</c:if> >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<select id="backCostWayId" name="backCostWayId" class="input_percent">
											<option value="0" <c:if test='${backCost.backCostWayId == 0}'>selected</c:if> >-----请选择-----</option>
											<c:forEach var="range" items="${ranges['33']}">
											<option value="${range.id}" <c:if test='${backCost.backCostWayId == range.id}'>selected</c:if> >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<input id="backDate" name="backDate" class="input_percent" type="text" value="<fmt:formatDate value='${backCost.backDate}' pattern='yyyy-MM-dd'/>" 
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly"/>
									</td>
									<td>
										<c:choose>
											<c:when test="${isViewAllBackCost == 1}">
											<input id="salePerson" name="salePerson" style="width: 75%;" type="text" value="${backCost.salePerson}" readonly="readonly"/>
											</c:when>
											<c:otherwise>${backCost.salePerson}</c:otherwise>
										</c:choose>
										<input id="salePersonId" name="salePersonId" type="hidden" value="${backCost.salePersonId}"/>
										<c:if test="${isViewAllBackCost == 1}">
										<input class="btn_select" type="button" value="" onclick="personDialog(${bc.index})"/>
										</c:if>
									</td>
									<td><input id="backCostDesc" name="backCostDesc" class="input_percent" type="text" value="${backCost.backCostDesc}"/></td>
								</tr>
								</c:forEach>
								<tr>
								
									<c:if test="${(isBusinessDeptBackCost == 1) || (isBusinessBackCost == 1)}">
									<td>
										<!--<input id="businessCheckbox" name="businessCheckbox" type="checkbox" onclick="changeValue(${bc.index},${bc.index})" value="1" 
											<c:if test='${backCost.businessStatus == 1 && backCost.grossBackCostId == 2}'>checked</c:if> <c:if test='${backCost.grossBackCostId != 2}'>disabled</c:if> />
										<input id="businessStatus" name="businessStatus" type="hidden" value="${backCost.businessStatus}"/>
									
										--><input id="businessCheckbox" name="businessCheckbox" type="checkbox" value="1" disabled="disabled" onclick="changeValue(${indexs1 },${indexs1 })"/>
										<input id="businessStatus" name="businessStatus" type="hidden" value="0"/>
									</td>
									</c:if>
									<td>
										<select class="input_percent" id="grossBackCostId" name="grossBackCostId" onchange="changeCheckbox(${indexs1 },${indexs1 })">
											<option value="0"  >----请选择----</option>
											<option value="1"  >成本</option>
											<option value="2"  >毛利</option>
										</select>
										<input id="id" name="id" type="hidden" value=""/>
									</td>
									<td><input id="cost" name="cost" class="input_percent" type="text" value="" onkeyup="onlyNumFloat(this)"/></td>
									<td>
										<select id="incomeTypeId" name="incomeTypeId" class="input_percent">
											<option value="0"  >-----请选择-----</option>
											<c:forEach var="range" items="${ranges['35']}">
											<option value="${range.id}"  >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<select id="backCostTypeId" name="backCostTypeId" class="input_percent">
											<option value="0"  >-----请选择-----</option>
											<c:forEach var="range" items="${ranges['32']}">
											<option value="${range.id}"  >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<select id="backCostWayId" name="backCostWayId" class="input_percent">
											<option value="0"  >-----请选择-----</option>
											<c:forEach var="range" items="${ranges['33']}">
											<option value="${range.id}"  >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<input id="backDate" name="backDate" class="input_percent" type="text" value="<fmt:formatDate value='<%=new java.util.Date()%>' pattern='yyyy-MM-dd'/>" 
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly"/>
									</td>
									<td>
										<c:choose>
											<c:when test="${isViewAllBackCost == 1}">
											<input id="salePerson" name="salePerson" style="width: 75%;" type="text" value="${BC.salePerson}" readonly="readonly"/>
											</c:when>
											<c:otherwise>${BC.salePerson}</c:otherwise>
										</c:choose>
										<input id="salePersonId" name="salePersonId" type="hidden" value="${BC.salePersonId}"/>
										<c:if test="${isViewAllBackCost == 1}">
										<input class="btn_select" type="button" value="" onclick="personDialog(${indexs1 })"/>
										</c:if>
									</td>
									<td><input id="backCostDesc" name="backCostDesc" class="input_percent" type="text" value=""/></td>
								</tr>
								
								<tr>
								
									<c:if test="${(isBusinessDeptBackCost == 1) || (isBusinessBackCost == 1)}">
									<td>
										<!--<input id="businessCheckbox" name="businessCheckbox" type="checkbox" onclick="changeValue(${bc.index},${bc.index})" value="1" 
											<c:if test='${backCost.businessStatus == 1 && backCost.grossBackCostId == 2}'>checked</c:if> <c:if test='${backCost.grossBackCostId != 2}'>disabled</c:if> />
										<input id="businessStatus" name="businessStatus" type="hidden" value="${backCost.businessStatus}"/>
									
										--><input id="businessCheckbox" name="businessCheckbox" type="checkbox" value="1" disabled="disabled" onclick="changeValue(${indexs2 },${indexs2 })"/>
										<input id="businessStatus" name="businessStatus" type="hidden" value="0"/>
									</td>
									</c:if>
									<td>
										<select class="input_percent" id="grossBackCostId" name="grossBackCostId" onchange="changeCheckbox(${indexs2 },${indexs2 })">
											<option value="0"  >----请选择----</option>
											<option value="1"  >成本</option>
											<option value="2"  >毛利</option>
										</select>
										<input id="id" name="id" type="hidden" value=""/>
									</td>
									<td><input id="cost" name="cost" class="input_percent" type="text" value="" onkeyup="onlyNumFloat(this)"/></td>
									<td>
										<select id="incomeTypeId" name="incomeTypeId" class="input_percent">
											<option value="0"  >-----请选择-----</option>
											<c:forEach var="range" items="${ranges['35']}">
											<option value="${range.id}"  >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<select id="backCostTypeId" name="backCostTypeId" class="input_percent">
											<option value="0"  >-----请选择-----</option>
											<c:forEach var="range" items="${ranges['32']}">
											<option value="${range.id}"  >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<select id="backCostWayId" name="backCostWayId" class="input_percent">
											<option value="0"  >-----请选择-----</option>
											<c:forEach var="range" items="${ranges['33']}">
											<option value="${range.id}"  >${range.rangeValue}</option>
											</c:forEach>
										</select>
									</td>
									<td>
										<input id="backDate" name="backDate" class="input_percent" type="text" value="<fmt:formatDate value='<%=new java.util.Date()%>' pattern='yyyy-MM-dd'/>" 
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly"/>
									</td>
									<td>
										<c:choose>
											<c:when test="${isViewAllBackCost == 1}">
											<input id="salePerson" name="salePerson" style="width: 75%;" type="text" value="${BC.salePerson}" readonly="readonly"/>
											</c:when>
											<c:otherwise>${BC.salePerson}</c:otherwise>
										</c:choose>
										<input id="salePersonId" name="salePersonId" type="hidden" value="${BC.salePersonId}"/>
										<c:if test="${isViewAllBackCost == 1}">
										<input class="btn_select" type="button" value="" onclick="personDialog(${indexs2 })"/>
										</c:if>
									</td>
									<td><input id="backCostDesc" name="backCostDesc" class="input_percent" type="text" value=""/></td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<input id="contractId" name="contractId" type="hidden" value="${contract.id}"/>
	<input id="sign" name="sign" type="hidden" value="${sign}"/>
</form>	 
</body>
</html>